﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace TeamWPF
{
    /// <summary>
    /// Interaction logic for IndividualWindow.xaml
    /// </summary>
    public partial class IndividualWindow : Window
    {
        private bool isWindowClosing = false;
        private bool canProcceed;

        public IndividualWindow()
        {
            InitializeComponent();
        }
        
        private CourierManager.IndividualCustomer senderClient;
        private CourierManager.Customer recepient;

        public void OnClosingIndividualWindow(object sender, CancelEventArgs e)
        {
            isWindowClosing = true;
        }

        private void IndividualIDNumberLostFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                CourierManager.ValidatorIDNumber.ValidatePersonalIDNumber(this.IndividualIDNumberInput.Text);
                CourierManager.IndividualCustomer tempCustomer =
                new CourierManager.IndividualCustomer(ulong.Parse( this.IndividualIDNumberInput.Text));
                tempCustomer.PathToDatabase = "../../../CourierManager/IndividualCustomers.txt";
                List<string> allIndividualClients = tempCustomer.ReadClientData();
                for (int record = 0; record < allIndividualClients.Count; record++)
                {
                    string[] dataArray = allIndividualClients[record].Split('*');
                    string currentIDNumber = dataArray[4];
                    if (this.IndividualIDNumberInput.Text == currentIDNumber)
                    {
                        this.IndividualFirstNameInput.Text = dataArray[1];
                        this.IndividualLastNameInput.Text = dataArray[2];
                        this.IndividualAddressInput.Text = dataArray[3];
                        this.IndividualPhoneInput.Text = dataArray[5];
                        break;
                    }
                }
            }

            catch (CourierManager.InvalidIDNumberException ide)
            {
                if (!isWindowClosing)
                {
                    this.IndividualIDNumberInput.Focusable = true;
                    Keyboard.Focus(this.IndividualIDNumberInput);
                    InvalidIDNumber newIDWindow = new InvalidIDNumber();
                    newIDWindow.ErrorText.Text = ide.Message;
                    newIDWindow.Show();
                }
            }
        }

        private void ProccessIndividualClient(object sender, RoutedEventArgs e)
        {
            if (this.SenderGrid.Visibility == System.Windows.Visibility.Visible)
            {
                if (!(String.IsNullOrWhiteSpace(this.IndividualFirstNameInput.Text) 
                    || String.IsNullOrWhiteSpace(this.IndividualLastNameInput.Text)
                    || String.IsNullOrWhiteSpace(this.IndividualIDNumberInput.Text)
                    ||String.IsNullOrWhiteSpace(this.IndividualPhoneInput.Text)
                    ||String.IsNullOrWhiteSpace(this.IndividualAddressInput.Text)))
                {
                    canProcceed = true;
                }
            }
            else if (this.CompanyRecepientGrid.Visibility == System.Windows.Visibility.Visible)
            {
                if (!(String.IsNullOrWhiteSpace(this.CompanyRecipientNameInput.Text)
                    ||String.IsNullOrWhiteSpace(this.CompanyRecipientAddressInput.Text)
                    ||String.IsNullOrWhiteSpace(this.CompanyRecipientPhoneInput.Text)))
                {
                    canProcceed = true;
                }
            }
            else if (this.IndividualRecepientGrid.Visibility == System.Windows.Visibility.Visible)
            {
                if (!(String.IsNullOrWhiteSpace(this.IndividualRecipientFirstNameInput.Text)
                    ||String.IsNullOrWhiteSpace(this.IndividualRecipientLastNameInput.Text)
                    ||String.IsNullOrWhiteSpace(this.IndividualRecipientAddressInput.Text)
                    ||String.IsNullOrWhiteSpace(this.IndividualRecipientPhoneInput.Text)))
                {
                    canProcceed = true;
                }
            }

            if (canProcceed == true)
            {
                if (this.SenderGrid.Visibility == System.Windows.Visibility.Visible)
                {
                    canProcceed = false;
                    ulong phone = ulong.Parse(CourierManager.ValidatorPhone.ValidatePhoneNumberFromString(this.IndividualPhoneInput.Text));
                    ulong idNumber = ulong.Parse(this.IndividualIDNumberInput.Text);
                    senderClient = new CourierManager.IndividualCustomer(this.IndividualFirstNameInput.Text, this.IndividualLastNameInput.Text,
                        this.IndividualAddressInput.Text, idNumber, phone);
                    //making the window form change from sender to recipient
                    //making the sender fields collapsed
                    this.SenderType.Visibility = System.Windows.Visibility.Collapsed;
                    this.IndividualForm.Visibility = System.Windows.Visibility.Collapsed;
                    this.SenderGrid.Visibility = System.Windows.Visibility.Collapsed;
                    //making the recipient fields visible
                    this.RecipientType.Visibility = System.Windows.Visibility.Visible;
                    this.RecipientTypeMenu.Visibility = System.Windows.Visibility.Visible;
                    this.CompanyRecepientGrid.Visibility = System.Windows.Visibility.Visible;
                }

                else if (this.SenderGrid.Visibility == System.Windows.Visibility.Collapsed
                    && (this.IndividualRecepientGrid.Visibility == System.Windows.Visibility.Visible
                    || this.CompanyRecepientGrid.Visibility == System.Windows.Visibility.Visible))
                {
                    if (this.RecipientMenuHeader.Header == IndividualRecipient.Header)
                    {
                        ulong phone = ulong.Parse(CourierManager.ValidatorPhone.ValidatePhoneNumberFromString(this.IndividualRecipientPhoneInput.Text));
                        recepient = new CourierManager.IndividualCustomer(this.IndividualRecipientFirstNameInput.Text, this.IndividualRecipientLastNameInput.Text,
                            this.IndividualRecipientAddressInput.Text, 0, phone);
                    }
                    else //(this.RecipientMenuHeader.Header == CompanyRecipient.Header)
                    {
                        ulong phone = ulong.Parse(CourierManager.ValidatorPhone.ValidatePhoneNumberFromString(this.CompanyRecipientPhoneInput.Text));
                        recepient = new CourierManager.CompanyCustomer(this.CompanyRecipientNameInput.Text, this.CompanyRecipientAddressInput.Text, 0,
                            phone);
                    }
                    canProcceed = false;
                    this.RecipientType.Visibility = System.Windows.Visibility.Collapsed;
                    this.RecipientTypeMenu.Visibility = System.Windows.Visibility.Collapsed;
                    this.IndividualForm.Visibility = System.Windows.Visibility.Collapsed;
                    this.SubmitIndividualData.Visibility = System.Windows.Visibility.Collapsed;
                    this.IndividualRecepientGrid.Visibility = System.Windows.Visibility.Collapsed;
                    this.CompanyRecepientGrid.Visibility = System.Windows.Visibility.Collapsed;
                    this.FreightGrid.Visibility = System.Windows.Visibility.Visible;
                } 
            }
            else
            {
                ErrorWindow emptyInputFieldsWindow = new ErrorWindow();
                emptyInputFieldsWindow.ErrorText.Text = "Empty input fields!";
                emptyInputFieldsWindow.Show();
            }
        }

        private void ChangeRecipientType(object sender, RoutedEventArgs e)
        {

            if (sender as MenuItem == IndividualRecipient)
            {
                this.RecipientMenuHeader.Header = IndividualRecipient.Header;
                //make company recepient fields collapse
                this.CompanyRecepientGrid.Visibility = System.Windows.Visibility.Collapsed;
                //make individual recepient fields visible
                this.IndividualRecepientGrid.Visibility = System.Windows.Visibility.Visible;
                
            }

            if (sender as MenuItem == CompanyRecipient)
            {
                this.RecipientMenuHeader.Header = CompanyRecipient.Header;
                //make individual recepient fields collapse
                this.IndividualRecepientGrid.Visibility = System.Windows.Visibility.Collapsed;
                //make company fields visible
                this.CompanyRecepientGrid.Visibility = System.Windows.Visibility.Visible;
                
            }
        }

        private void ProccessFreightData(object sender, RoutedEventArgs e)
        {
            if (!String.IsNullOrWhiteSpace(this.FreightWeightInput.Text))
            {
                canProcceed = true;
            }
            if (canProcceed == true)
            {
                CourierManager.Freight currentFreight;
                decimal weight = decimal.Parse(this.FreightWeightInput.Text);
                CourierManager.Location location = (CourierManager.Location)Enum.Parse(typeof(CourierManager.Location), this.comboboxLocation.SelectedValue.ToString());

                if (HasFilledDimensionsInput(this.FreightWidthInput.Text, this.FreightHeightInput.Text, this.FreightDepthInput.Text) == true)
                {
                    decimal width = decimal.Parse(this.FreightWidthInput.Text);
                    decimal height = decimal.Parse(this.FreightHeightInput.Text);
                    decimal depth = decimal.Parse(this.FreightDepthInput.Text);
                    currentFreight = new CourierManager.Freight(weight, senderClient, recepient, width, height, depth, location);
                    MessageBox.Show("Freight submitted sucessfully!");
                    this.Close();
                }
                else
                {
                    currentFreight = new CourierManager.Freight(weight, senderClient, recepient, location);
                    MessageBox.Show("Freight submitted sucessfully!");
                    this.Close();
                }
                currentFreight.PathToDatabase = @"../../../CourierManager/ListOfBillOfLadings.txt";
                currentFreight.WriteInDatabase(); 
            }
            else
            {
                ErrorWindow emptyInputFieldsWindow = new ErrorWindow();
                emptyInputFieldsWindow.ErrorText.Text = "Empty weight input field!";
                emptyInputFieldsWindow.Show();
            }
        }

        private bool HasFilledDimensionsInput(string width, string height, string depth)
        {
            if (String.IsNullOrEmpty(width) || String.IsNullOrEmpty(height) || String.IsNullOrEmpty(depth))
            {
                return false;
            }
            return true;
        }

        private void IndividualLastNameLostFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                string lastName = this.IndividualLastNameInput.Text;
                CourierManager.ValidatorName.PersonalLastNameValidator(lastName);
            }
            catch (CourierManager.InvalidNameException ex)
            {
                if (!isWindowClosing)
                {
                    this.IndividualLastNameInput.Focusable = true;
                    Keyboard.Focus(this.IndividualLastNameInput);
                    ErrorWindow errorWindow = new ErrorWindow();
                    errorWindow.ErrorText.Text = ex.Message;
                    errorWindow.Show();
                }
            }
        }

        private void IndividualFirstNameLostFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                string firstName = this.IndividualFirstNameInput.Text;
                CourierManager.ValidatorName.PersonalFirstNameValidator(firstName);
            }
            catch (CourierManager.InvalidNameException ex)
            {
                if (!isWindowClosing)
                {
                    this.IndividualFirstNameInput.Focusable = true;
                    Keyboard.Focus(this.IndividualFirstNameInput);
                    ErrorWindow errorWindow = new ErrorWindow();
                    errorWindow.ErrorText.Text = ex.Message;
                    errorWindow.Show();
                }
            }
        }

        private void IndividualPhoneLostFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                string individualPhone = this.IndividualPhoneInput.Text;
                CourierManager.ValidatorPhone.ValidatePhoneNumberFromString(individualPhone);
            }
            catch (CourierManager.InvalidPhoneNumberException phoneNumberEx)
            {
                if (!isWindowClosing)
                {
                    this.IndividualPhoneInput.Focusable = true;
                    Keyboard.Focus(this.IndividualPhoneInput);
                    ErrorWindow errorWindow = new ErrorWindow();
                    errorWindow.ErrorText.Text = phoneNumberEx.Message;
                    errorWindow.Show(); 
                }
            }
        }

        private void CompanyRecipientPhoneLostFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                string companyPhone = this.CompanyRecipientPhoneInput.Text;
                CourierManager.ValidatorPhone.ValidatePhoneNumberFromString(companyPhone);
            }
            catch (CourierManager.InvalidPhoneNumberException phoneEx)
            {
                if (!isWindowClosing)
                {
                    this.CompanyRecipientPhoneInput.Focusable = true;
                    Keyboard.Focus(this.CompanyRecipientPhoneInput);
                    ErrorWindow errorWindow = new ErrorWindow();
                    errorWindow.ErrorText.Text = phoneEx.Message;
                    errorWindow.Show(); 
                }
            }
        }

        private void CompanyRecipientNameLostFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                string companyName = this.CompanyRecipientNameInput.Text;
                CourierManager.ValidatorName.CompanyNameValidator(companyName);
            }
            catch (CourierManager.InvalidNameException nameEx)
            {
                if (!isWindowClosing)
                {
                    this.CompanyRecipientNameInput.Focusable = true;
                    Keyboard.Focus(this.CompanyRecipientNameInput);
                    ErrorWindow errorWindow = new ErrorWindow();
                    errorWindow.ErrorText.Text = nameEx.Message;
                    errorWindow.Show(); 
                }
            }
        }

        private void IndividualRecipientFirstNameLostFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                string firstName = this.IndividualRecipientFirstNameInput.Text;
                CourierManager.ValidatorName.PersonalFirstNameValidator(firstName);
            }
            catch (CourierManager.InvalidNameException nameEx)
            {
                if (!isWindowClosing)
                {
                    this.IndividualRecipientFirstNameInput.Focusable = true;
                    Keyboard.Focus(this.IndividualRecipientFirstNameInput);
                    ErrorWindow errorWindow = new ErrorWindow();
                    errorWindow.ErrorText.Text = nameEx.Message;
                    errorWindow.Show(); 
                }
            }
        }

        private void IndividualRecipientLastNameLostFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                string lastName = this.IndividualRecipientLastNameInput.Text;
                CourierManager.ValidatorName.PersonalLastNameValidator(lastName);
            }
            catch (CourierManager.InvalidNameException lastNameEx)
            {
                if (!isWindowClosing)
                {
                    this.IndividualRecipientLastNameInput.Focusable = true;
                    Keyboard.Focus(this.IndividualRecipientLastNameInput);
                    ErrorWindow errorWindow = new ErrorWindow();
                    errorWindow.ErrorText.Text = lastNameEx.Message;
                    errorWindow.Show(); 
                }
            }
        }

        private void IndividualRecipientPhoneLostFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                string individualRecepientPhone = this.IndividualRecipientPhoneInput.Text;
                individualRecepientPhone = CourierManager.ValidatorPhone.ValidatePhoneNumberFromString(individualRecepientPhone);
            }
            catch (CourierManager.InvalidPhoneNumberException phoneEx)
            {
                if (!isWindowClosing)
                {
                    this.IndividualRecipientPhoneInput.Focusable = true;
                    Keyboard.Focus(this.IndividualRecipientPhoneInput);
                    ErrorWindow errorWindow = new ErrorWindow();
                    errorWindow.ErrorText.Text = phoneEx.Message;
                    errorWindow.Show(); 
                }
            }
        }
    }
}
